<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui-v2.6.8/layui-v2.6.8/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                <a class="layui-btn" id="daochu"> 导出xls </a>
                <a class="layui-btn" id="daoru"> 导入xlsx </a>
            </div>
             </script>
            <form class="layui-form" lay-filter="layuiform">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label required">名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" lay-verify="required" lay-reqtext="名称不能为空"
                                placeholder="请输入名称" value="" class="layui-input">
                        </div>
                    </div>


                    <div class="layui-inline">
                        <label class="layui-form-label">卡号</label>
                        <div class="layui-input-inline">
                            <input name="cardnumber" placeholder="请输入图标" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">手机号</label>
                        <div class="layui-input-inline">
                            <input type="phone" name="phone" placeholder="请输入路径" value="" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">注册日期范围</label>
                    <div class="layui-inline" id="test-range">
                        <input type="text" id="startDate" name="registration_q" class="layui-input" placeholder="开始日期">
                        <input type="text" id="endDate" name="registration_h" class="layui-input" placeholder="结束日期">
                    </div>
                </div>
                <a class="layui-btn" id="searchBtn">搜索</a>
            </form>


            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        </div>
    </div>
    <script src="../layui-v2.6.8/layui-v2.6.8/layui/layui.js" charset="utf-8"></script>
    <script src="../js/t03.js"></script>
    <script>

        layui.use(['laydate', 'form', 'table','upload'], function () {


            var $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate,
                 upload = layui.upload;

            var t03Table = table.render({
                elem: '#currentTableId',
                url: host + '/transfer',
                type: 'get',
                toolbar: '#toolbarDemo',
                
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                    
                }],
                cols: [[
                    { type: "checkbox", width: 50 },
                    { field: 'id', width: 80, title: 'ID', sort: true },
                    { field: 'name', width: 80, title: '名称' },
                    { field: 'cardnumber', width: 80, title: '卡号', sort: true },
                    { field: 'phone', width: 80, title: '手机号' },
                    { field: 'balance', title: '金额', minWidth: 150 },
                    { field: 'avatar', title: '图片', minWidth: 150 },
                    { field: 'registration', title: '时间', minWidth: 150 },
                    { title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
                ]],
             
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true
            
            });

            $("#daochu").on("click", function () {
                console.log("导出");
                // 提示用户正在导出
                window.location.href = host + '/export'

            });

            $("#searchBtn").on("click", function () {
                console.log("搜索");

                var formData = form.val('layuiform'); // 获取表单数据 
                var result = JSON.stringify(formData); // 将表单数据转换为 JSON 字符串
                t03Table.reload({
                    where: formData
                })
                // 执行 AJAX 请求
                // $.ajax({
                //     url: host + '/transfer/user', // 确保这个 URL 是正确的
                //     type: 'GET', // 或 'POST' 根据你的需求
                //     data: formData, // 使用获取的表单数据作为请求参数
                //     success: function (response) {
                //         console.log(response);
                //         // 
                //     },
                //     error: function (xhr, status, error) {
                //         console.error('请求失败:', status, error);
                //     },

                // });
            });
            

       laydate.render({
                elem: '#test-range' //开始时间和结束时间所在 input 框的父选择器
                //设置开始日期、日期日期的 input 选择器
                , range: ['#startDate', '#endDate'] //数组格式为 layui 2.6.6 开始新增
            });

            //执行实例
            var uploadInst = upload.render({
                elem: '#daoru' //绑定元素
                , accept: 'file' //指定允许上传的文件类型
                , url: host + 'import' //上传接口
                , done: function (res) {
                    //上传完毕回调
                }
                , error: function () {
                    //请求异常回调
                }
            });
   

        


            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../page/table/adduser.html',
                        end: function () {
                            console.log("关闭");
                            t03Table.reload()
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作

                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;
                    layer.alert("确定删除吗");
                    ids = data.map(function (item) {
                        return item.id
                    })

                    $.ajax({
                        url: host + '/transfer/batchDelete?ids=' + ids,
                        type: 'delete',
                        success: function (res) {
                            console.log(res);
                            if (res.code === 200) {
                                layer.msg("删除成功");
                                t03Table.reload()
                            } else {
                                layer.alert("删除失败");
                            }
                        }

                    });
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {

                    var index = layer.open({
                        title: '编辑用户',
                        type: 2,
                        shade: 0.2,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../page/table/puduser.html',
                        success: function (layero, index) {
                            console.log("编辑页面已经打开");
                            var body = layer.getChildFrame('body', index);
                            body.find("input[name=id]").val(obj.data.id)
                            body.find("input[name=name]").val(obj.data.name)
                            body.find("input[name=cardnumber]").val(obj.data.cardnumber);
                            body.find("input[name=phone]").val(obj.data.phone);
                            body.find("input[name=balance]").val(obj.data.balance);
                            body.find("input[name=avatar]").val(obj.data.avatar);
                            body.find("input[name=registration]").val(obj.data.registration)
                            console.log(layero, index);
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除行么', function (index) {
                        // obj.del();

                        $.ajax({
                            url: host + '/transfer/' + obj.data.id,
                            type: 'delete',
                            success: function (res) {
                                console.log(res);
                                if (data.code === 200) {
                                    layer.msg("删除成功");
                                    t03Table.reload()
                                } else {
                                    layer.alert("删除失败");
                                }
                            }
                        })
                        // layer.close(index);
                    });
                }
            });


     });

        
    </script>

</body>

</html>